package com.huidian.resource.client.enhance;

import com.huidian.resource.client.MeterFeign;
import com.huidian.resource.client.dto.MeterDto;
import com.huidian.resource.client.enums.EmBaseCacheDef;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.CollectionUtils;
import java.util.List;


/**
 * @author tjs
 */
public class MeterFeignEnhance implements MeterFeign {

    private MeterFeign meterFeign;

    public MeterFeignEnhance(MeterFeign meterFeign){
        this.meterFeign = meterFeign;
    }

    @Override
    public List<MeterDto> selectList(MeterDto meterDto) {
        return meterFeign.selectList(meterDto);
    }


    @Override
    public MeterDto selectById(Long meterId) {
        return meterFeign.selectById(meterId);
    }

    @Cacheable(value = EmBaseCacheDef.EM_METER_ID_CACHE, key = EmBaseCacheDef.EM_METER_SPEL_ID_KEY,cacheManager = EmBaseCacheDef.EM_METER_CACHE_MANAGER,condition = "#meterId != null ")
    public MeterDto getMeterByMeterId(Long meterId){
        return meterFeign.selectById(meterId);
    }

    @Cacheable(value = EmBaseCacheDef.EM_METER_INDEX_CACHE, key = "'asId:'.concat(#asId)",cacheManager = EmBaseCacheDef.EM_METER_CACHE_MANAGER,condition = "#asId != null ")
    public List<MeterDto>  getMeterListByAsId(Long asId){
        MeterDto emMeterReq = new MeterDto();
        emMeterReq.setAsId(asId);
        return meterFeign.selectList(emMeterReq);
    }

    @Cacheable(value = EmBaseCacheDef.EM_METER_INDEX_CACHE, key = "'gwId:'.concat(#gwId)",cacheManager = EmBaseCacheDef.EM_METER_CACHE_MANAGER,condition = "#gwId != null ")
    public List<MeterDto>  getMeterListByGwId(Integer gwId){
        MeterDto emMeterReq = new MeterDto();
        emMeterReq.setGwId(gwId);
        return meterFeign.selectList(emMeterReq);
    }
    @Cacheable(value = EmBaseCacheDef.EM_METER_INDEX_CACHE, key = "'iotId:'.concat(#iotId).concat(#slave != null ?':slave:'.concat(#slave):'')",cacheManager = EmBaseCacheDef.EM_METER_CACHE_MANAGER,condition = "#iotId != null ")
    public MeterDto getMeterByIotIdSlave(String iotId, String slave){
        MeterDto emMeterReq = new MeterDto();
        emMeterReq.setIotId(iotId);
        emMeterReq.setSlave(slave);
        List<MeterDto> emMeterRespList = meterFeign.selectList(emMeterReq);
        return !CollectionUtils.isEmpty(emMeterRespList) ? emMeterRespList.get(0) : null;
    }

}
